# http://wiki.openqa.org/display/WTR/HTML+report+class

class HtmlReporter
  # Initialize the report class


  def initialize()
    @overallResult = 'PASSED'
    @reportContent1 = ''
    @reportContent2 = ''
    @tcNbr = 1
  end

  # Create a report
  def createReport(reportName)
    # Get current time
    t = Time.now
    strTime = t.strftime("_%Y%m%d_%H%M%S.html")
    strNiceTime = t.strftime("%m/%d/%Y @ %I:%M:%S %p")
    @strYear = t.year.to_s
    
    # Create the report name
    strTotalReport = reportName + strTime
    strTestCaseName = File.basename(reportName)
    strReportNameShort = File.basename(strTotalReport)
    # Create the HTML report
    strFile = File.open(strTotalReport, 'w')

    #Get the username
    case $hostOS
    when "mswin32" then @username = ENV['UserName']
    when /linux/   then  @username = ENV['LOGNAME']
    when /darwin/  then @username = ENV['LOGNAME']
    else @username = "#{@hostOS} not handled."
    end
      

    # Format the header of the HTML report
    @reportContent1 = '<html>
      <head>
      <meta content=text/html; charset=ISO-8859-1 http-equiv=content-type>
      <script src="assets/sorttable.js"></script>
      <script src="assets/EmailAFriend.js"></script>
      <title>QA Test Report</title>
      <link rel="stylesheet" type="text/css" href="assets/cobbled.css" />
      </head>
      <body>
      <br>
      <center>
      <table width=800 border=0 cellpadding=2 cellspacing=2>
      <tbody>
      <tr>
      <td>
      <table width=100% border=0 cellpadding=2 cellspacing=2>
      <tbody>
      <tr>
      <td style=width: 150px;><img src="assets/Cobbled.png"/></td>
      <td align=right><p class=title>QA Test Report</p></td>
      </tr>
      </tbody>
      </table>
      <div align=right><a onClick="window.print()" href="" ><img src="assets/print_icon.gif" alt="Print"/></a>
      <a onClick="javascript:mailpage()" href="" ><img src="assets/mail_icon.gif" alt="Email" /></a></div>
      <hr width=100% class=border size=1px>
      <br>
      <br>
      <center>
      <table border=0 width=95% cellpadding=2 cellspacing=2>
      <tbody>
      <tr>
      <td width=20%><p class=bold_text>Test Case Name</p></td>
      <td width=5%><p class=bold_text>:</p></td>
      <td width=75%><p class=normal_text>' + strTestCaseName + '</p></td>
      </tr>
      <tr>
      <td width=20%><p class=bold_text>Report Name</p></td>
      <td width=5%><p class=bold_text>:</p></td>
      <td width=75%><p class=normal_text>' + strReportNameShort + '</p></td>
      </tr>
      <tr>
      <td width=20%><p class=bold_text>Test Execution</p></td>
      <td width=5%><p class=bold_text>:</p></td>
      <td width=75%><p class=normal_text>' + strNiceTime + '</p></td>
      </tr>
      <tr>
      <td width=20%><p class=bold_text>Test Executed By</p></td>
      <td width=5%><p class=bold_text>:</p></td>
      <td width=75%><p class=normal_text>' + @username + '</p></td>
      </tr>
      <tr>
      <td width=20%><p class=bold_text>Overall Result</p></td>
      <td width=5%><p class=bold_text>:</p></td>'

    @reportContent2 = '</tr>
      </tbody>
      </table>
      </center>
      <br><br>
      <center>
      <table width=95% cellpadding=2 cellspacing=0 class="sortable">
      <tbody>
      <tr>
      <td class=bborder_left width=5%>#</td>
      <td class=bborder_left width=15%>Command</td>
      <td class=bborder_left width=10%>Result</td>
      <td class=bborder_right width=35%>Parameters</td>
      <td class=bborder_right width=35%>Expected</td>
      </tr>'

    # Close the report
    strFile.close

    return strTotalReport
  end

  def addtoReport(reportName, command, result, parameters, expected)
    # Format the body of the HTML report
    case result
    when "PASSED"
      @reportContent2 = @reportContent2 + '<tr><td class=border_left_center width=5%><p class=normal_text>' + @tcNbr.to_s + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_left><p class=normal_text>' + command + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=result_ok>' + result + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=normal_text>' + parameters + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=normal_text>' + expected + '</p></td>'
    when "FAILED"
      @overallResult = 'FAILED'
      @reportContent2 = @reportContent2 + '<tr><td class=border_left_center width=5%><p class=normal_text>' + @tcNbr.to_s + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_left><p class=normal_text>' + command + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=result_nok>' + result + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=normal_text>' + parameters + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=normal_text>' + expected + '</p></td>'
    when "HEADING"
      @reportContent2 = @reportContent2 + '<tr><td class=lborder_left>' + @tcNbr.to_s + '</td><td class=lborder_left><p>&nbsp;</p></td><td class=lborder_left><p>&nbsp;</p></td><td class=lborder_right><p>&nbsp;</p></td><td class=lborder_right><p>&nbsp;</p></td>'
    else
      @reportContent2 = @reportContent2 + '<tr><td class=border_left_center><p class=normal_text>' + @tcNbr.to_s + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_left><p class=normal_text>' + command + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=result_unk>' + result + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=normal_text>' + parameters + '</p></td>'
      @reportContent2 = @reportContent2 + '<td class=border_right><p class=normal_text>' + expected + '</p></td>'
    end
    @tcNbr += 1
   
  end

  def finishReport(reportName)
    # Open the HTML report
    strFile = File.open(reportName, 'a')

    # Format the footer of the HTML report
    @reportContent2 = @reportContent2 + "<tfoot><tr>
      <td class=bborder_left><p>&nbsp;</p></td>
      <td class=bborder_left><p>&nbsp;</p></td>
      <td class=bborder_left><p>&nbsp;</p></td>
      <td class=bborder_right><p>&nbsp;</p></td>
      <td class=bborder_right><p>&nbsp;</p></td>
      </tr>
      </table>
      <br><br>
      <hr width=100% class=border size=1px>
      <center><p class=small_text>This page contains confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibit.<br>&copy;#{@strYear} Robert H. Koertge Jr.<br>All rights reserved.</p></center>
      <br>"

    strFile.puts(@reportContent1)

    if (@overallResult == 'PASSED')
      strFile.puts('<td width=75%><p class=overall_ok>' + @overallResult + '</p></td>')
    else
      strFile.puts('<td width=75%><p class=overall_nok>' + @overallResult + '</p></td>')
    end

    strFile.puts(@reportContent2)

    # Close the report
    strFile.close
  end

end
